<template>
  <div class="mod-config">
    <el-form :inline="true">
      <el-form-item>
        <date-phase ref="datePhase" dateTypeRange="034" :datePhase="datePhase" ></date-phase>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getReport()" :loading="dataListLoading">查询</el-button>
        <el-button type="primary" @click="exportExcel">导出</el-button>
        <el-button v-if="isSaleLeader" @click="setBaseCustomerNum()">总客户基数设置</el-button>
      </el-form-item>
    </el-form>
      <el-table :data="reportItems" style="width: 100%;" id="sale-table">
          <el-table-column
            prop="datePhase"
            label="时间"
            min-width="120">
          </el-table-column>
        <el-table-column
          prop="newCustomerNum"
          label="新建客户数"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="totalCustomerNum"
          label="总客户数"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="dealCustomerNum"
          label="成交总客户数"
          min-width="100">
        </el-table-column>
        <!-- <el-table-column
          prop="starCustomerNum"
          label="三星客户数"
          min-width="100">
        </el-table-column> -->
        <el-table-column
          prop="piNum"
          label="PI数"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="dealNewCustomerNum"
          label="成交新客户数"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="newCustomerValue"
          label="新客户业绩"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="dealOldCustomerNum"
          label="成交老客户数"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="oldCustomerValue"
          label="老客户业绩"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="valueTotal"
          label="总业绩"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="adCost"
          label="广告费"
          min-width="100">
        </el-table-column>
        <el-table-column
          prop="acos"
          label="ACOS%"
          min-width="100">
        </el-table-column>
      </el-table>
    <el-dialog
      title="设置"
      :visible.sync="dialogVisible"
      width="300">
      <div>
        总客户基数： <el-input-number class="edit-cell" style="width:200px" :precision="0" :controls="false" v-model="baseCustomerNum" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveBaseCustomerNum">保存</el-button>
        <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    </div>
</template>

<script>
  import DatePhase from  '@/components/sale/date-phase'
  import dingMixin from  '@/mixins/ding'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        datePhase:{
            dateType: 3,
            value:'',
            to:''
        },
        baseCustomerNum: 0,
        dialogVisible:false,
        dingId:'',
        editMode: false,
        dataListLoading: false,
        saveLoading:false,
        reportItems:[]
      }
    },
    mixins: [dingMixin],
    components: {
      DatePhase
    },
    activated () {
      //this.getDataList()

    },
    created(){
      this.getBaseCustomerNum()
    },
    methods: {
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(document.getElementById('sale-table'));
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'b2b-m.xlsx')
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      },
      setBaseCustomerNum(){
        this.dialogVisible = true
      },
      saveBaseCustomerNum(){
        this.$http({
          url: this.$http.adornUrl('/sale/setBaseCustomerNum'),
          method: 'get',
          params: this.$http.adornParams({
            num: this.baseCustomerNum
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({message: '保存成功', type: 'info', duration: 3000})
            this.dialogVisible = false
          } else {
            this.$message({message: '保存失败', type: 'error', duration: 3000})
          }
        })
      },
      getBaseCustomerNum(){
        this.$http({
          url: this.$http.adornUrl('/sale/getBaseCustomerNum'),
          method: 'get',
          params: this.$http.adornParams({
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.baseCustomerNum = data.data
          } else {
            this.baseCustomerNum = 0
          }
        })
      },
      // 获取数据列表
      getReport () {
        if(this.datePhase.value == null || this.datePhase.value == ''){
          this.$message({message: '请选择时间', type: 'error', duration: 3000})
          return
        }
        this.editMode = false
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sale/getSummaryVo'),
          method: 'get',
          params: this.$http.adornParams({
            'datePhase': this.datePhase.value,
            'dateType': this.datePhase.dateType,
            'to': this.datePhase.to,
            'dingId': this.dingId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.reportItems = data.data
          } else {
            this.reportItems = []
          }
        }).finally(()=>{
          this.dataListLoading = false
        })
      }
    }
  }
</script>
<style lang="scss">

  .el-table{
    font-size: 12px;
  }

  .edit-cell{
    width: 60px;
  }

  .el-input-number.is-without-controls .el-input__inner{
    padding: 0;
    text-align: left;
  }

</style>
